package oocl.eshop.picserver.impl;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

public class LoadPicture implements Runnable {

	Socket sk = null;

	public LoadPicture(Socket sk) {
		super();
		this.sk = sk;
	}

	@Override
	public void run() {
		
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(sk.getInputStream(), "utf8"));
			String requestLine = reader.readLine();
//			System.out.println(requestLine);
			OutputStream out = sk.getOutputStream();
			out.write("HTTP/1.0 200 OK\r\n".getBytes());
			out.write("\r\n".getBytes());
			String url = requestLine.split("\\s")[1];
			url="D:\\Kinva"+url;
			String filePath = url;
//			System.out.println(filePath);
			InputStream inputStream = new FileInputStream(filePath);
			byte[] buf = new byte[inputStream.available()];
			inputStream.read(buf);
			out.write(buf);
			out.close();
			inputStream.close();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
